package com.grasp.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.grasp.activity.R;
import com.grasp.adapter.IconAdapter;
import com.grasp.custom.view.ButtomDialog;
import com.grasp.data.IconItem;

public class HelperUtil {
	private static HelperUtil mDialogUtil;

	public static HelperUtil initDialogUtil() {
		if (null == mDialogUtil) {
			mDialogUtil = new HelperUtil();
		}
		return mDialogUtil;
	}

	/**
	 * 选择图标Dialog
	 * 
	 * @param mContext
	 * @param mOnClickIconItem
	 */
	public void selectIconDialog(Context mContext,
			final OnClickIconItem mOnClickIconItem) {
		final List<IconItem> items = getIconItems();
		final ButtomDialog dialog = new ButtomDialog(mContext, R.style.MyDialog);
		IconAdapter adapter = new IconAdapter(mContext, items);
		dialog.setAdapter(adapter);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setDialogAnim(R.style.BottomDialogAnim);
		dialog.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				mOnClickIconItem.selectIconItem(items.get(position));
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	/**
	 * 获取本地图标名称
	 * 
	 * @return
	 */
	private List<IconItem> getIconItems() {
		List<IconItem> items = new ArrayList<IconItem>();
		for (int i = 1; i <= 10; i++) {
			IconItem item = new IconItem();
			if (i >= 100) {
				item.setFileName("icon_" + i);
			} else if (i >= 10) {
				item.setFileName("icon_0" + i);
			} else {
				item.setFileName("icon_00" + i);
			}
			item.setTitle("");
			items.add(item);
		}
		return items;
	}

	public static int findIdByName(Context mContext, String iconName) {
		return mContext.getResources().getIdentifier(iconName, "drawable",
				mContext.getPackageName());
	}

	public interface OnClickIconItem {
		void selectIconItem(IconItem iconItem);
	}
}
